package com.hxzy.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.constant.UserConstants;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.SysDictData;
import com.hxzy.common.core.domain.entity.SysDictType;
import com.hxzy.common.core.domain.entity.SysRole;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.utils.DictUtils;
import com.hxzy.common.utils.StringUtils;
import com.hxzy.system.domain.dto.SysDictTypeSearchDTO;
import com.hxzy.system.domain.dto.ValueExistsDTO;
import com.hxzy.system.mapper.SysDictDataMapper;
import com.hxzy.system.mapper.SysDictTypeMapper;
import com.hxzy.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典类型表
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {


    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Override
    public IPage<SysDictType> selectDictTypeList(SysDictTypeSearchDTO sysDictTypeSearchDTO) {

        IPage<SysRole> page= PageDTO.of(sysDictTypeSearchDTO.getPageNum(),sysDictTypeSearchDTO.getPageSize());
        return  this.baseMapper.search(page,sysDictTypeSearchDTO);
    }

    /**
     * 根据所有字典类型
     * @return
     */
    @Override
    public List<SysDictType> selectDictTypeAll() {
        return this.baseMapper.selectDictTypeAll();
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            return dictDatas;
        }
        dictDatas = this.dictDataMapper.selectDictDataByType(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    /**
     * 根据字典类型查询信息
     * @param dictType 字典类型
     * @return
     */
    @Override
    public SysDictType selectDictTypeByType(String dictType) {
        return this.baseMapper.selectDictTypeByType(dictType);
    }

    /**
     * 批量删除字典类型信息
     * @param dictIds 需要删除的字典ID
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDictTypeByIds(Long[] dictIds) {
        for (Long dictId : dictIds)
        {
            SysDictType dictType = this.getById(dictId);
            if (this.dictDataMapper.countDictDataByType(dictType.getDictType()) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }
            this.removeById(dictId);
            //移出redis缓存数据
            DictUtils.removeDictCache(dictType.getDictType());
        }
    }

    @Override
    public void loadingDictCache() {

        Map<String, List<SysDictData>> dictDataMap = this.dictDataMapper.selectAllDictDataList("0").stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        for (Map.Entry<String, List<SysDictData>> entry : dictDataMap.entrySet())
        {
            DictUtils.setDictCache(entry.getKey(), entry.getValue().stream().sorted(Comparator.comparing(SysDictData::getDictSort)).collect(Collectors.toList()));
        }
    }

    /**
     * 清空字典在redis中的缓存
     */
    @Override
    public void clearDictCache() {
        DictUtils.clearDictCache();
    }

    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache() {
        clearDictCache();
        loadingDictCache();
    }

    /**
     * 校验字典类型称是否唯一
     * @param valueExistsDTO 字典类型
     * @return
     */
    @Override
    public int checkDictTypeUnique(ValueExistsDTO valueExistsDTO) {

        int count=this.baseMapper.checkDictTypeUnique(valueExistsDTO);
         return count;
    }

    /**
     * 导出excel
     * @param sysDictTypeSearchDTO
     * @return
     */
    @Override
    public List<SysDictType> searchExport(SysDictTypeSearchDTO sysDictTypeSearchDTO) {
        return this.baseMapper.searchExport(sysDictTypeSearchDTO);
    }
}
